package com.flyco.modules.pms.service.impl;

import com.flyco.modules.pms.entity.PmsProductAttributeValue;
import com.flyco.modules.pms.mapper.PmsProductAttributeValueMapper;
import com.flyco.modules.pms.model.PmsProductAttributeModel;
import com.flyco.modules.pms.service.IPmsProductAttributeValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 产品参数信息
 * @Author: flyco
 * @Date:   2019-11-27
 * @Version: V1.0
 */
@Service
public class PmsProductAttributeValueServiceImpl extends ServiceImpl<PmsProductAttributeValueMapper, PmsProductAttributeValue> implements IPmsProductAttributeValueService {

    @Autowired
    private PmsProductAttributeValueMapper pmsProductAttributeValueMapper;

    @Override
    public void deleteByAttributeId(Long id) {
        pmsProductAttributeValueMapper.deleteByAttributeId(id);
    }

    @Override
    public List<PmsProductAttributeModel> selectAttributeByCategoryId(Long productId, Long attributeCategoryId){
        return pmsProductAttributeValueMapper.selectAttributeByCategoryId(productId, attributeCategoryId);
    }

    @Override
    public void saveFromProduct(Long productId, List<PmsProductAttributeModel> list) {
        List<PmsProductAttributeValue> values = modelToEntity(productId, list);
        if (null != values)
            this.saveOrUpdateBatch(values);
    }

    private List<PmsProductAttributeValue> modelToEntity(Long productId, List<PmsProductAttributeModel> list){
        List<PmsProductAttributeValue> values = new ArrayList<>();
        for (PmsProductAttributeModel model : list) {
            PmsProductAttributeValue value = new PmsProductAttributeValue();
            value.setId(model.getProductAttributeValueId());
            value.setProductId(productId);
            value.setProductAttributeId(model.getId());
            value.setValue(model.getValue());
            values.add(value);
        }
        return values;
    }
}
